<?php

class RegistrationController extends Controller
{
	public $defaultAction = 'registration';

	private $returnUrl = "";
	
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return (isset($_POST['ajax']) && $_POST['ajax']==='registration-form')?array():array(
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
		),
		);
	}
	/**
	 * Registration user
	 */
	public function actionRegistration() {
		$this->returnUrl=array("/client/suiteInscriptionUser");
		$this->registration();
	}

	/**
	 * Registration user
	 */
	public function actionRegistrationDepuisPanier() {
		$this->returnUrl=array("/client/create");
		$this->registration();
	}

	private function registration() {
		$model = new RegistrationForm;
		$profile=new Profile;
		$profile->regMode = true;
		// max_size upload
		ini_set ( "memory_limit", "10M");

		// ajax validator
		if(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')
		{
			echo UActiveForm::validate(array($model,$profile));
			Yii::app()->end();
		}
			
		if (Yii::app()->user->id) {
			$this->redirect(Yii::app()->controller->module->profileUrl);
		} else {
			if(isset($_POST['RegistrationForm'])) {
				$model->attributes=$_POST['RegistrationForm'];
				$profile->attributes=((isset($_POST['Profile'])?$_POST['Profile']:array()));

				$file_image = CUploadedFile::getInstance($model,'urlImage');

				if ( (is_object($file_image) && get_class($file_image)==='CUploadedFile')){
					// chemin d'acces de l'image
					$model->urlImage = Yii::app()->params['ImageProfilsFolder'].'/'.$file_image;
				}

				if($model->validate()&&$profile->validate())
				{
					$soucePassword = $model->password;
					$model->activkey=UserModule::encrypting(microtime().$model->password);
					$model->password=UserModule::encrypting($model->password);
					$model->verifyPassword=UserModule::encrypting($model->verifyPassword);
					$model->createtime=time();
					$model->lastvisit=((Yii::app()->controller->module->loginNotActiv||(Yii::app()->controller->module->activeAfterRegister&&Yii::app()->controller->module->sendActivationMail==false))&&Yii::app()->controller->module->autoLogin)?time():0;
					$model->superuser=0;
					$model->status=((Yii::app()->controller->module->activeAfterRegister)?User::STATUS_ACTIVE:User::STATUS_NOACTIVE);

					if ($model->save()) {
						if (is_object($file_image)){

							$folder=Yii::app()->basePath.'/../'.Yii::app()->params['ImageProfilsFolder'];

							//Enregistrement de l' image
							$file_image->saveAs($folder.'/'.$file_image);
						}
							
						if (Yii::app()->controller->module->sendActivationMail) {
							$activation_url = $this->createAbsoluteUrl('/user/activation/activation',array("activkey" => $model->activkey, "email" => $model->email));
							UserModule::sendMail($model->email,UserModule::t("You registered from {site_name}",array('{site_name}'=>Yii::app()->name)),UserModule::t("Please activate you account go to {activation_url}",array('{activation_url}'=>$activation_url)));
						}
							
						if ((Yii::app()->controller->module->loginNotActiv||(Yii::app()->controller->module->activeAfterRegister&&Yii::app()->controller->module->sendActivationMail==false))&&Yii::app()->controller->module->autoLogin) {
							$identity=new UserIdentity($model->username,$soucePassword);
							$identity->authenticate();
							Yii::app()->user->login($identity,0);
							$this->redirect($this->returnUrl);
						} else {
							if (!Yii::app()->controller->module->activeAfterRegister&&!Yii::app()->controller->module->sendActivationMail) {
								Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Contact Admin to activate your account."));
							} elseif(Yii::app()->controller->module->activeAfterRegister&&Yii::app()->controller->module->sendActivationMail==false) {
								Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please {{login}}.",array('{{login}}'=>CHtml::link(UserModule::t('Login'),Yii::app()->controller->module->loginUrl))));
							} elseif(Yii::app()->controller->module->loginNotActiv) {
								Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please check your email or login."));
							} else {
								Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please check your email."));
							}
							$this->refresh();
						}
					}
				} else $profile->validate();
			}
			$this->render('/user/registration',array('model'=>$model,'profile'=>$profile));
		}
	}
}